<?php

namespace Modules\Article\Entities;

use Houdunwang\Arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //那些字段可以添加到数据库
    // protected $fillable = [];
    //除了谁不添加到数据库
    protected $guarded = ['_token'];


    /**
     * @Notes: 获取所有的栏目，并且是带有层级的
     * @Interface:          getAll
     * @param        null  $cate
     * @return:             array
     * @author:             Jokenzhang
     * @Time:               2020/12/21  14:23
     */
    public function getAll($cate = null)
    {
        $data = $this->query()->get()->toArray();
        if (!is_null($cate)) {
            foreach ($data as $k => &$d) {
                $d['_selected'] = $cate['pid'] == $d['id'];
                $d['_disabled'] = $cate['id'] == $d['pid'] || (new Arr())->isChild($data, $d['id'], $cate['pid'], 'id');
            }
        }
        //引入了后盾人的arr组件   houndunwang/arr  树形结构
        $data = (new Arr())->tree($data, 'name', 'id');
        return $data;
    }

    /**
     * @Notes: 是否有子栏目
     * @Interface:          isChildCategory
     * @return:             bool
     * @author:             Jokenzhang
     * @Time:               2020/12/21  14:24
     */
    public function hasChildCategory(){
        $data = $this->query()->get()->toArray();
        return (new Arr()) -> hasChild($data, $this->id);
    }
}
